﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Runtime.InteropServices;
using System.Threading;
using System.Configuration;

namespace 橘子树chat
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    /// 
    
    public partial class MainWindow : Window
    {
        [DllImport("maindll.dll", EntryPoint = "Login", CallingConvention = CallingConvention.Cdecl)]
        public static extern bool Login(String UserName, String UserPassword);
        [DllImport("maindll.dll", EntryPoint = "Registry", CallingConvention = CallingConvention.Cdecl)]
        public static extern bool Registry(String UserName, String UserPassword);
        
        Point _pressedPosition;
        bool _isDragMoved = false;
        private System.Windows.Threading.DispatcherTimer dt = new DispatcherTimer();
        void dt_Tick(object sender, EventArgs e)
        {
            // 要执行的任务
            toast_MissingInformation.Visibility = Visibility.Hidden;
            text_MissingInformation.Visibility = Visibility.Hidden;
            dt.Stop();
        }



        public MainWindow()
        {
            
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
            InitializeComponent();
            string UserName = ConfigurationSettings.AppSettings["UserName"];
            string UserPassword = ConfigurationSettings.AppSettings["UserPassword"];
            textBox.Text = UserName;
            passwordBox.Password = UserPassword;

        }
        void Window_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            _pressedPosition = e.GetPosition(this);
        }

        void Window_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (Mouse.LeftButton == MouseButtonState.Pressed && _pressedPosition != e.GetPosition(this))
            {
                _isDragMoved = true;
                DragMove();
            }

        }

        void Window_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (_isDragMoved)
            {
                _isDragMoved = false;
                e.Handled = true;
            }
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void button_Copy1_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
            
        }
        private void showMissingInformationControl()
        {
            dt.Stop();
            toast_MissingInformation.Visibility = Visibility.Visible;
            text_MissingInformation.Visibility = Visibility.Visible;

            dt.Interval = TimeSpan.FromMilliseconds(2500);
            dt.Tick += new EventHandler(dt_Tick);
            dt.Start();
        }
        String message;
        Thread thread;
        public void showMessage()
        {
            this.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate ()

            {
                textBlock.Text = message;

                //Margin margin
                Thickness margin = new Thickness();
                margin.Left = 10;
                margin.Top = 80;
                //margin.
                textBlock.Margin = margin;
                textBlock.Opacity = 0;
                textBlock.Visibility = Visibility.Visible;
                
            });
            for(int i = 0; i <= 100; i++)
            {
                this.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate ()

                {
                    if (textBlock.Opacity <= 100 && i%2==0)
                    {
                        textBlock.Opacity += 0.08;
                    }
                    if (!(textBlock.Margin.Top <= 30))
                    {
                        Thickness mar = textBlock.Margin;
                        mar.Top -= 3;
                        textBlock.Margin = mar;
                    }
                    this.InvalidateVisual();
                    if(textBlock.Opacity>=100 && textBlock.Margin.Top <= 30)
                    {
                        i = 100;
                    }

                });
                System.Threading.Thread.Sleep(35);
            }
            //System.Threading.Thread.Sleep(3000);
            for (int i = 0; i <= 300; i++)
            {
                this.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate ()

            {
                if (textBlock.Opacity <= 100)
                {
                    textBlock.Opacity -=0.3;
                }
               
                this.InvalidateVisual();

            });
                System.Threading.Thread.Sleep(35);
            }

        }

        private void button1_Copy_Click(object sender, RoutedEventArgs e)
        {
            if(textBox.Text!="" && passwordBox.Password!="")
            {
                //showMessage("在登录过程中发生错误，这可能是因为您的密码或账户输入错误亦或网络连接失败。");
                if ((bool)checkBox.IsChecked)
                {
                    Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    cfa.AppSettings.Settings["UserName"].Value = textBox.Text;
                    cfa.AppSettings.Settings["UserPassword"].Value = passwordBox.Password;
                    cfa.Save();
                }

                
                
                
                
                //继续执行逻辑
                bool result=Login(textBox.Text, passwordBox.Password);



                //result = true;



                if (result)
                {
                    //MessageBox.Show("成功了耶！");
                    /*validation val = new validation();
                    val.Owner = this;
                    bool ValiResult = (bool)val.ShowDialog();
                    if (ValiResult != true)
                    {
                        message = "已取消验证，登录未通过";
                        if (thread != null)
                        {
                            thread.Abort();
                        }
                        thread = new Thread(showMessage);

                        thread.Start();
                    }
                    else
                    {
                        
                    }*/
                    //this.Close();
                    ChatView chat = new ChatView();
                    chat.Show();
                    this.Close();
                }
                else
                {
                    message = "在登录过程中发生错误，这可能是因为您的密码或账户输入错误亦或网络连接失败。";
                    if (thread != null)
                    {
                        thread.Abort();
                    }
                    thread = new Thread(showMessage);

                    thread.Start();
                }


                

            }
            else
            {
                showMissingInformationControl();  
            }
            
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (textBox.Text != "" && passwordBox.Password != "")
            {
                //继续执行逻辑
                bool result=Registry(textBox.Text, passwordBox.Password);
                if (result)
                {
                    //button1_Copy_Click(null, null);
                    message = "注册成功啦qwq！";
                    if (thread != null)
                    {
                        thread.Abort();
                    }
                    thread = new Thread(showMessage);

                    thread.Start();
                }
                else
                {
                    message = "在注册过程中发生错误，这可能是因为无法连接到服务器。Q^Q";
                    if (thread != null)
                    {
                        thread.Abort();
                    }
                    thread = new Thread(showMessage);

                    thread.Start();
                }

            }
            else
            {
                showMissingInformationControl();
            }
        }

        private void label2_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            About dlg = new About();
            dlg.Owner = this;
            dlg.ShowDialog();
        }
    }
}
